package com.orion.schdule;

import com.orion.util.SmallTool;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 *  spring的schedule线程池默认只有一个线程,现在自行设置schedule线程池(大于一个)，
 *
 *  执行是同步，就算大于指定间隔，也是执行一轮归一轮，看例子三就知道，taskExecutor才是异步(Async注解)
 *
 *  多个定时任务指定间隔执行
 *
 * @author Administrator
 */
@Component
@ConditionalOnProperty(value = "schedule.multiScheduleWithCustomPoolTask",havingValue = "true")
public class MultiScheduleWithCustomSchedulePoolTask {

    /**
     * 每隔2s执行一次
     */
    @Scheduled(cron = "0/2 * * * * ? ")
    public void JustScheduleForA() {
        SmallTool.printTimeAndThread("begin Just Schedule Task A ");
        SmallTool.sleepMillis(4000);
        SmallTool.printTimeAndThread("end Just Schedule Task A -------");
    }

    /**
     * 每隔2s执行一次
     */
    @Scheduled(cron = "0/2 * * * * ? ")
    public void JustScheduleForB() {
        SmallTool.printTimeAndThread("begin Just Schedule Task B ");
        SmallTool.sleepMillis(4000);
        SmallTool.printTimeAndThread("end Just Schedule Task B -------");
    }

    /**
     * A、B都没有睡眠的时候，A、B互不影响
     *
     1644737894027	|	32	|	orion-scheduler-1	|	begin Just Schedule Task B
     1644737894027	|	33	|	orion-scheduler-2	|	begin Just Schedule Task A
     1644737896005	|	32	|	orion-scheduler-1	|	begin Just Schedule Task B
     1644737896005	|	33	|	orion-scheduler-2	|	begin Just Schedule Task A
     1644737898002	|	33	|	orion-scheduler-2	|	begin Just Schedule Task B
     1644737898002	|	37	|	orion-scheduler-4	|	begin Just Schedule Task A
     1644737900016	|	32	|	orion-scheduler-1	|	begin Just Schedule Task B
     1644737900016	|	36	|	orion-scheduler-3	|	begin Just Schedule Task A
     1644737902003	|	36	|	orion-scheduler-3	|	begin Just Schedule Task B
     1644737902003	|	32	|	orion-scheduler-1	|	begin Just Schedule Task A
     1644737904011	|	38	|	orion-scheduler-5	|	begin Just Schedule Task A
     1644737904011	|	36	|	orion-scheduler-3	|	begin Just Schedule Task B
     1644737906008	|	33	|	orion-scheduler-2	|	begin Just Schedule Task B
     1644737906008	|	38	|	orion-scheduler-5	|	begin Just Schedule Task A

     A睡秒4s，超过间隔时间，A、B互不影响,A内部都不会影响自己下一次任务间隔2秒（换B睡眠也是同理）
     1644744806022	|	32	|	orion-scheduler-1	|	begin Just Schedule Task A
     1644744806022	|	33	|	orion-scheduler-2	|	begin Just Schedule Task B
     1644744808008	|	32	|	orion-scheduler-1	|	begin Just Schedule Task A
     1644744810008	|	36	|	orion-scheduler-3	|	begin Just Schedule Task A
     1644744810024	|	33	|	orion-scheduler-2	|	end Just Schedule Task B -------
     1644744812004	|	32	|	orion-scheduler-1	|	begin Just Schedule Task B
     1644744812004	|	33	|	orion-scheduler-2	|	begin Just Schedule Task A
     1644744814016	|	33	|	orion-scheduler-2	|	begin Just Schedule Task A
     1644744816010	|	32	|	orion-scheduler-1	|	end Just Schedule Task B -------
     1644744816010	|	33	|	orion-scheduler-2	|	begin Just Schedule Task A
     1644744818006	|	36	|	orion-scheduler-3	|	begin Just Schedule Task A
     1644744818006	|	33	|	orion-scheduler-2	|	begin Just Schedule Task B
     1644744820002	|	36	|	orion-scheduler-3	|	begin Just Schedule Task A
     1644744822004	|	37	|	orion-scheduler-4	|	begin Just Schedule Task A
     1644744822020	|	33	|	orion-scheduler-2	|	end Just Schedule Task B -------

     各自都睡眠4s，自定义线程池有多个线程，互不影响，但是都是执行完才下一轮，不是异步，是同步

     1644744940019	|	33	|	orion-scheduler-2	|	begin Just Schedule Task B
     1644744940019	|	32	|	orion-scheduler-1	|	begin Just Schedule Task A
     1644744944025	|	33	|	orion-scheduler-2	|	end Just Schedule Task B -------
     1644744944025	|	32	|	orion-scheduler-1	|	end Just Schedule Task A -------
     1644744946011	|	32	|	orion-scheduler-1	|	begin Just Schedule Task A
     1644744946011	|	33	|	orion-scheduler-2	|	begin Just Schedule Task B
     1644744950024	|	32	|	orion-scheduler-1	|	end Just Schedule Task A -------
     1644744950024	|	33	|	orion-scheduler-2	|	end Just Schedule Task B -------
     1644744952005	|	37	|	orion-scheduler-4	|	begin Just Schedule Task B
     1644744952005	|	36	|	orion-scheduler-3	|	begin Just Schedule Task A
     1644744956020	|	37	|	orion-scheduler-4	|	end Just Schedule Task B -------
     1644744956020	|	36	|	orion-scheduler-3	|	end Just Schedule Task A -------

     */
}
